//
//  UIActionSheet+DF.m
//  换换圈
//
//  Created by 艾柯斯 on 14-12-27.
//  Copyright (c) 2014年 艾柯斯. All rights reserved.
//

#import "UIActionSheet+DF.h"
#import <objc/runtime.h>



@interface UIActionSheet() <UIActionSheetDelegate>


typedef void (^ClickedButtonBlock)(UIActionSheet* actionSheet,NSInteger);

@property (nonatomic) ClickedButtonBlock clickedButtonBlock;

@end


@implementation UIActionSheet (DF)


+ (void)showInView:(UIView *)view clickedButton:(void (^)(UIActionSheet*,NSInteger))clickedButton title:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
    
 
    //UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:nil cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:nil];
    
    va_list args;
    va_start( args, otherButtonTitles);
    if(otherButtonTitles) {
        while ((otherButtonTitles = va_arg(args,id))) {
            NSLog(@"%@",otherButtonTitles);
            // [actionSheet addButtonWithTitle:otherButtonTitles];
        }
    }
 
    va_end(args);
   // [actionSheet showInView:view clickedButton:clickedButton];

}

-(void)showInView:(UIView *)view clickedButton:(void (^)(UIActionSheet*,NSInteger))clickedButton {
    self.clickedButtonBlock = clickedButton;
    self.delegate = self;
    [self showInView:view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(self.clickedButtonBlock) {
        self.clickedButtonBlock(self,buttonIndex);
    }
}


#pragma mark - 运行时相关
static char clickedButtonBlockKey;

-(void)setClickedButtonBlock:(ClickedButtonBlock)clickedButtonBlock {
    objc_setAssociatedObject(self, &clickedButtonBlockKey,
                             clickedButtonBlock,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


-(ClickedButtonBlock)clickedButtonBlock {
    return objc_getAssociatedObject(self, &clickedButtonBlockKey);
}




@end
